#include "chartviewex.h"
#include <QPainter>
#include <QPointF>
#include <QValueAxis>
#include <QAbstractAxis>
#include <QLegendMarker>
#include <QMessageBox>
#include <QIcon>

#include <QGraphicsLayout>
#include <QFileDialog>
#include <QDebug>

ChartViewEx::ChartViewEx(QWidget *parent)
    :QChartView(parent)
{
    mZoomFactor = 1.8;
    isPress =false;
    isHoverd = false;
    isHoveredEnable = false;

    m_tooltip = NULL;
    m_rubberBand = NULL;
    currentSeries = currentHorverSeries = NULL;

    mChart=new QChart();
    mChart->legend()->setAlignment(Qt::AlignRight);
    mChart->layout()->setContentsMargins(0,0,0,0);
    //mChart->legend()->setAcceptedMouseButtons(Qt::AllButtons);

    QPen pen;
    pen.setWidth(1);
    pen.setStyle(Qt::DashLine);
    pen.setColor(QColor(96,96,96));

    xAxis = new QValueAxis;
    xAxis->setRange(0,1);
    xAxis->setGridLinePen(pen);
    xAxis->setTickCount(11);

    yAxis = new QValueAxis;
    yAxis->setRange(0,1);
    yAxis->setGridLinePen(pen);
    yAxis->setTickCount(6);
    yAxis->setReverse(true);

    mChart->setAxisX(xAxis);
    mChart->setAxisY(yAxis);

    setChart(mChart);
    createActions();
    createDialog();

    setRenderHint(QPainter::Antialiasing);
}

void ChartViewEx::createActions()
{
    popMenu = new QMenu();
    setCurvePropertyAction = new QAction("Chart Property",this);
    popMenu->addAction(setCurvePropertyAction);

    popMenu->addSeparator();

    isHoveredEnable = false;
    hoveredEnableAction = new QAction("Hovered Enable",this);
    hoveredEnableAction->setCheckable(true);
    hoveredEnableAction->setChecked(isHoveredEnable);
    popMenu->addAction(hoveredEnableAction);

    isMarkerVisible = false;
    markerAction = new QAction("Marker Visible",this);
    markerAction->setCheckable(true);
    markerAction->setChecked(isMarkerVisible);
    popMenu->addAction(markerAction);

    isLegendVisible = true;
    legendVisibleAction = new QAction("Legend Visible",this);
    legendVisibleAction->setCheckable(true);
    legendVisibleAction->setChecked(isLegendVisible);
    popMenu->addAction(legendVisibleAction);

    popMenu->addSeparator();

    selectAllCurve = new QAction("Display All",this);
    popMenu->addAction(selectAllCurve);

    hideAllCurve = new QAction("Hide All",this);
    popMenu->addAction(hideAllCurve);

    popMenu->addSeparator();

    saveImgAction = new QAction("Save Image",this);
    popMenu->addAction(saveImgAction);

    connect(setCurvePropertyAction,&QAction::triggered,this,&ChartViewEx::showSetPropertyDialog);
    connect(saveImgAction,&QAction::triggered,this,&ChartViewEx::saveImage);
    connect(legendVisibleAction,&QAction::triggered,this,&ChartViewEx::legendVisible);
    connect(selectAllCurve,&QAction::triggered,this,&ChartViewEx::SelectAllNorNot);
    connect(hideAllCurve,&QAction::triggered,this,&ChartViewEx::SelectAllNorNot);
    connect(markerAction,&QAction::triggered,this,&ChartViewEx::markerVisible);
    connect(hoveredEnableAction,&QAction::triggered,this,&ChartViewEx::hoveredEnable);
}

void ChartViewEx::createDialog()
{
    curveDialog = new QDialog();
    curveDialog->setWindowTitle("Curve Property");

    lbLineName = new QLabel("Name:");
    lbLineType = new QLabel("Type:");
    lbLineWidth = new QLabel("Width:");
    lbColor = new QLabel("Color:");

    spinBoxLineWidth =  new QSpinBox;
    spinBoxLineWidth->setRange(1,10);

    lELineName = new QLineEdit;

    cbBoxLineType = new QComboBox;
    cbBoxLineType->addItem("Solid");
    cbBoxLineType->addItem("Dash");
    cbBoxLineType->addItem("Dot");
    cbBoxLineType->addItem("DashDot");
    cbBoxLineType->addItem("DashDotDot");
    //cbBoxLineType->addItem("CustomDash");

    cbBoxLineColor = new QComboBox;
    fillColorList(cbBoxLineColor);
    QGridLayout *layout1=new QGridLayout;
    layout1->addWidget(lbLineName,0,0);
    layout1->addWidget(lELineName,0,1);
    layout1->addWidget(lbLineWidth,1,0);
    layout1->addWidget(spinBoxLineWidth,1,1);
    layout1->addWidget(lbLineType,2,0);
    layout1->addWidget(cbBoxLineType,2,1);
    layout1->addWidget(lbColor,3,0);
    layout1->addWidget(cbBoxLineColor);

    layout1->setSizeConstraint(QLayout::SetFixedSize);
    curveDialog->setLayout(layout1);

    chartDialog = new QDialog();
    chartDialog->setWindowTitle("Chart Property");

    lbChartBackground = new QLabel("Background:");
    lbLegendAlign = new QLabel("Legend Align:");
    lbChartTitle = new QLabel("Title:");
    lbXAixsName = new QLabel("X Axis:");
    lbYAxisName = new QLabel("Y Axis:");
    lbZoomFactor = new QLabel("Zoom Factor:");

    cbBoxBkColor = new QComboBox;
    fillColorList(cbBoxBkColor);

    cbBoxLegendAlign = new QComboBox;
    cbBoxLegendAlign->addItem("Top");
    cbBoxLegendAlign->addItem("Left");
    cbBoxLegendAlign->addItem("Right");
    cbBoxLegendAlign->addItem("Bottom");
    cbBoxLegendAlign->addItem("None");

    lEChartTitle = new QLineEdit;
    lEXAxisName = new QLineEdit;
    lEYAxisName = new QLineEdit;
    spinBoxZoomFactor = new QDoubleSpinBox;
    spinBoxZoomFactor->setRange(1.0,2.0);
    spinBoxZoomFactor->setSingleStep(0.1);

    QGridLayout *layout2=new QGridLayout;
    layout2->addWidget(lbChartTitle,0,0);
    layout2->addWidget(lEChartTitle,0,1);
    layout2->addWidget(lbChartBackground,1,0);
    layout2->addWidget(cbBoxBkColor,1,1);
    layout2->addWidget(lbLegendAlign,2,0);
    layout2->addWidget(cbBoxLegendAlign,2,1);
    layout2->addWidget(lbXAixsName,3,0);
    layout2->addWidget(lEXAxisName,3,1);
    layout2->addWidget(lbYAxisName,4,0);
    layout2->addWidget(lEYAxisName,4,1);
    layout2->addWidget(lbZoomFactor,5,0);
    layout2->addWidget(spinBoxZoomFactor,5,1);

    layout2->setSizeConstraint(QLayout::SetFixedSize);
    chartDialog->setLayout(layout2);

    connect(lEChartTitle,&QLineEdit::textChanged,this,&ChartViewEx::setChartName);
    connect(lEXAxisName,&QLineEdit::textChanged,this,&ChartViewEx::setXAxisName);
    connect(lEYAxisName,&QLineEdit::textChanged,this,&ChartViewEx::setYAxisName);
    connect(cbBoxLegendAlign,SIGNAL(currentIndexChanged(int)),this,SLOT(getLegendAlign(int)));
    connect(lELineName,SIGNAL(textChanged(QString)),this,SLOT(getLineName(QString)));
    connect(cbBoxLineType,SIGNAL(currentIndexChanged(int)),this,SLOT(getLineType(int)));
    connect(spinBoxLineWidth,SIGNAL(valueChanged(int)),this,SLOT(getLineWidth(int)));
    connect(cbBoxBkColor,SIGNAL(currentIndexChanged(int)),this,SLOT(getColor(int)));
    connect(cbBoxLineColor,SIGNAL(currentIndexChanged(int)),this,SLOT(getColor(int)));
}

void ChartViewEx::fillColorList(QComboBox *cbBox)
{
    QStringList colorList = QColor::colorNames();
    foreach (QString color, colorList) {
        QPixmap pix(QSize(100,20));
        pix.fill(QColor(color));
        cbBox->addItem(QIcon(pix),NULL);
        cbBox->setIconSize(QSize(100,20));
        cbBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    }
}

void ChartViewEx::contextMenuEvent(QContextMenuEvent *event)
{
    int displayCount = 0;
    int hideCount = 0;

    QList<QLegendMarker*> lstMarker = mChart->legend()->markers();
    for(int i=0;i<lstMarker.count();i++)
    {
        QLegendMarker* marker = lstMarker.at(i);
        switch (marker->type()) {
        case QLegendMarker::LegendMarkerTypeXY:
        {
            if(isLegendVisible)
            {
                if(marker->pen().color().alphaF() == 1.0f)
                {
                    displayCount ++;
                }
                else
                {
                    hideCount ++;
                }
            }
            else
            {
                if(marker->series()->isVisible())
                {
                    displayCount ++;
                }
                else
                {
                    hideCount ++;
                }
            }

            break;
        }
        case QLegendMarker::LegendMarkerTypeArea:
        case QLegendMarker::LegendMarkerTypeBar:
        case QLegendMarker::LegendMarkerTypePie:
        case QLegendMarker::LegendMarkerTypeBoxPlot:
        case QLegendMarker::LegendMarkerTypeCandlestick:
        default:
        {
            break;
        }
        }
    }

    if(displayCount == mChart->series().count())
    {
        selectAllCurve->setEnabled(false);
    }
    else
    {
        selectAllCurve->setEnabled(true);
    }

    if(hideCount == mChart->series().count())
    {
        hideAllCurve->setEnabled(false);
    }
    else
    {
        hideAllCurve->setEnabled(true);
    }

    popMenu->exec(QCursor::pos());
    event->accept();
}

void ChartViewEx::mouseMoveEvent(QMouseEvent *event)
{
    if(isPress)
    {
        posEnd = event->pos();
        checkPointValid(posEnd);

        if(m_rubberBand)
            m_rubberBand->setGeometry(QRect(posStart.toPoint(),posEnd.toPoint()).normalized());
    }

    QChartView::mouseMoveEvent(event);
}

void ChartViewEx::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        if(mChart->plotArea().contains(event->pos()))
        {
            if(currentHorverSeries)
                setHoveredSigal(posCur,false);

            setCursor(Qt::CrossCursor);
            isPress = true;
            posStart = posEnd = event->pos();

            if(!m_rubberBand)
                m_rubberBand = new QRubberBand(QRubberBand::Rectangle,this);
            m_rubberBand->setGeometry(QRect(posStart.toPoint(),QSize()));
            m_rubberBand->show();
        }
    }

    QChartView::mousePressEvent(event);
}

void ChartViewEx::mouseReleaseEvent(QMouseEvent *event)
{
    if((event->button() == Qt::LeftButton))
    {
        if(isPress)
        {
            if(m_rubberBand)
                m_rubberBand->hide();

            QRectF rect1(posStart,posEnd);
            QRectF rect2 = rect1.normalized();

            if(rect1 == rect2)
            {
                QPointF pos = event->pos();
                checkPointValid(pos);

                QRectF rect;
                rect.setTopLeft(posStart);
                rect.setBottomRight(pos);
                mChart->zoomIn(rect);
            }
            else
            {
                mChart->zoomReset();
            }
        }

        setCursor(Qt::ArrowCursor);
        isPress =false;
    }

    QChartView::mouseReleaseEvent(event);
}

void ChartViewEx::wheelEvent(QWheelEvent *e)
{
    if(currentHorverSeries)
        setHoveredSigal(posCur,false);

    if(e->delta()>0)
    {
        mChart->zoom(1/mZoomFactor);
        mChart->zoomIn();
    }
    else
    {
        mChart->zoom(mZoomFactor);
        mChart->zoomOut();
    }
}

void ChartViewEx::AddCurveData(CCurveData *data)
{
    QList<float> pX = data->GetData("X");
    QList<float> pY = data->GetData("Y");
    if(pX.count() != pY.count())
    {
        QMessageBox::warning(this,"Warning","There are some error in the data!");
        return;
    }

    QLineSeries *series = new QLineSeries;
    QList<QPointF> lstP;
    if( mChart->series().count()==0)
    {
        mXMax =mXMin = pX[0];
        mYMax = mYMin  = pY[0];
    }
    for(int i=0;i<pX.count();i++)
    {
        lstP.append(QPointF(pX[i],pY[i]));

        mXMax = pX[i] > mXMax ? pX[i]:mXMax;
        mXMin = pX[i] < mXMin ? pX[i]:mXMin;
        mYMax = pY[i] > mYMax ? pY[i]:mYMax;
        mYMin = pY[i] < mYMin ? pY[i]:mYMin;
    }

    series->setName(data->GetCurveName());

    series->append(lstP);
    connect(series,&QLineSeries::hovered,this,&ChartViewEx::setHoveredSigal);
    connect(series,&QLineSeries::doubleClicked,this,&ChartViewEx::showSetPropertyDialog);

    mChart->addSeries(series);

    series->setColor(data->GetLineColor());

    mChart->axisX()->setRange(mXMin, mXMax);

    if(mYMax == mYMin)
    {
        mChart->axisY()->setRange(mYMin - 0.05, mYMax + 0.05);
    }
    else
    {
        mChart->axisY()->setRange(mYMin - (mYMax - mYMin) *0.05, mYMax + (mYMax - mYMin) * 0.05);
    }

    mChart->setAxisX(xAxis,series);
    mChart->setAxisY(yAxis,series);
    QList<QLegendMarker*> lstMarker = mChart->legend()->markers(series);
    foreach (QLegendMarker* marker, lstMarker) {
        connect(marker, &QLegendMarker::clicked, this, &ChartViewEx::handleMarkerClicked);
    }
}

void ChartViewEx::legendVisible()
{
    isLegendVisible = !isLegendVisible;

    QList<QLegendMarker*> lstMarker = mChart->legend()->markers();
    for(int i=0;i<lstMarker.count();i++)
    {
        QLegendMarker* marker = lstMarker.at(i);
        switch (marker->type()) {
        case QLegendMarker::LegendMarkerTypeXY:
        {
            if(!marker->series()->isVisible())
            {
                marker->setVisible(isLegendVisible);
            }
            break;
        }
        case QLegendMarker::LegendMarkerTypeArea:
        case QLegendMarker::LegendMarkerTypeBar:
        case QLegendMarker::LegendMarkerTypePie:
        case QLegendMarker::LegendMarkerTypeBoxPlot:
        case QLegendMarker::LegendMarkerTypeCandlestick:
        default:
        {
            break;
        }
        }
    }
}

void ChartViewEx::handleMarkerClicked()
{
    QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
    switch (marker->type()) {
    case QLegendMarker::LegendMarkerTypeXY:
    {
        marker->series()->setVisible(!marker->series()->isVisible());
        marker->setVisible(isLegendVisible);

        qreal aplha =1.0;
        if(!marker->series()->isVisible())
        {
            aplha = 0.5;
        }

        QBrush brush;
        brush= marker->brush();
        QColor color;
        color= brush.color();
        color.setAlphaF(aplha);
        brush.setColor(color);
        marker->setBrush(brush);

        QPen pen;
        pen= marker->pen();
        color = pen.color();
        color.setAlphaF(aplha);
        pen.setColor(color);
        marker->setPen(pen);
        break;
    }
    case QLegendMarker::LegendMarkerTypeArea:
    case QLegendMarker::LegendMarkerTypeBar:
    case QLegendMarker::LegendMarkerTypePie:
    case QLegendMarker::LegendMarkerTypeBoxPlot:
    case QLegendMarker::LegendMarkerTypeCandlestick:
    default:
    {
        break;
    }
    }
}

void ChartViewEx::hoveredEnable()
{
    isHoveredEnable = !isHoveredEnable;

//    if(isHoveredEnable)
//    {
//        for(int i=0;i<mChart->series().count();i++)
//        {
//            connect(qobject_cast<QLineSeries*> (mChart->series().at(i)),&QLineSeries::hovered,this,&ChartViewEx::setHoveredSigal);
//        }
//    }
//    else
//    {
//        for(int i=0;i<mChart->series().count();i++)
//        {
//            disconnect(qobject_cast<QLineSeries*> (mChart->series().at(i)),&QLineSeries::hovered,this,&ChartViewEx::setHoveredSigal);
//        }
//    }
}

void ChartViewEx::setHoveredSigal(const QPointF &point, bool state)
{
    if(!isHoveredEnable)
        return;

    QLineSeries *series = qobject_cast<QLineSeries*> (sender());
    if(series != NULL)
    {
        if(state)
        {
            currentHorverSeries = series;
            setCursor(Qt::CrossCursor);
            QPen pen = currentHorverSeries->pen();
            pen.setWidth(2*pen.width());
            currentHorverSeries->setPen(pen);

            if(!m_tooltip)
            {
                m_tooltip = new CallOut(mChart);
            }

            m_tooltip->setAnchor(point);
            m_tooltip->setZValue(11);
            m_tooltip->updateGeometry();
            m_tooltip->show();
        }
        else
        {
            if(currentHorverSeries !=NULL)
            {
                setCursor(Qt::ArrowCursor);
                QPen pen = currentHorverSeries->pen();
                pen.setWidth(pen.width()/2);
                currentHorverSeries->setPen(pen);

                m_tooltip->hide();
                currentHorverSeries = NULL;
            }
        }
    }
    else
    {
        if(currentHorverSeries !=NULL)
        {
            setCursor(Qt::ArrowCursor);
            QPen pen = currentHorverSeries->pen();
            pen.setWidth(pen.width()/2);
            currentHorverSeries->setPen(pen);

            m_tooltip->hide();
            currentHorverSeries = NULL;
        }
    }
}

void ChartViewEx::showSetPropertyDialog()
{
    currentSeries = qobject_cast<QLineSeries*> (sender());
    if(currentSeries == NULL)
    {
        lEChartTitle->setText(mChart->title());
        lEXAxisName->setText(mChart->axisX()->titleText());
        lEYAxisName->setText(mChart->axisY()->titleText());

        if(!mChart->legend()->isVisible())
        {
            cbBoxLegendAlign->setCurrentIndex(cbBoxLegendAlign->count()-1);
        }
        else
        {
            Qt::Alignment align = mChart->legend()->alignment();
            QString strFind;
            switch (align) {
            case Qt::AlignTop:
                strFind = "Top";
                break;
            case Qt::AlignLeft:
                strFind = "Left";
                break;
            case Qt::AlignBottom:
                strFind = "Bottom";
                break;
            case Qt::AlignRight:
                strFind = "Right";
                break;
            default:
                break;
            }
            for(int i=0;i<cbBoxLegendAlign->count()-1;i++)
            {
                if(cbBoxLegendAlign->itemText(i).contains(strFind,Qt::CaseInsensitive))
                {
                    cbBoxLegendAlign->setCurrentIndex(i);
                    break;
                }
            }
        }

        //        QStringList colorList = QColor::colorNames();
        //        for(int i=0;i<colorList.count();i++)
        //        {
        //            QColor color(colorList[i]);

        //            if(color.name() == mChart->plotAreaBackgroundBrush().color())
        //            {
        //                cbBoxBkColor->setCurrentIndex(i);
        //                break;
        //            }
        //        }


        spinBoxZoomFactor->setValue(mZoomFactor);

        chartDialog->setModal(true);
        chartDialog->show();
    }
    else
    {
        lELineName->setText(currentSeries->name());
        QPen pen = currentSeries->pen();
        spinBoxLineWidth->setValue(pen.width());
        Qt::PenStyle style= pen.style();
        cbBoxLineType->setCurrentIndex((int)style-1);

        QStringList colorList = QColor::colorNames();
        for(int i=0;i<colorList.count();i++)
        {
            QColor color(colorList[i]);
            if(color.name() == currentSeries->color().name())
            {
                cbBoxLineColor->setCurrentIndex(i);
                break;
            }
        }

        curveDialog->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Dialog);
        curveDialog->setModal(false);
        curveDialog->show();
    }
}

void ChartViewEx::getLegendAlign(int index)
{
    Qt::Alignment align;
    switch (index) {
    case 0:
        align = Qt::AlignTop;
        break;
    case 1:
        align = Qt::AlignLeft;
        break;
    case 2:
        align   = Qt::AlignRight;
        break;
    case 3:
        align = Qt::AlignBottom;
        break;
    case 4:
        mChart->legend()->hide();
    default:
        break;
    }

    if(index != cbBoxLegendAlign->count()-1)
    {
        mChart->legend()->setVisible(true);
        mChart->legend()->setAlignment(align);
    }
}

void ChartViewEx::getLineType(int index)
{
    index = index + 1;
    QPen pen = currentSeries->pen();
    pen.setStyle((Qt::PenStyle)index);
    currentSeries->setPen(pen);
}

void ChartViewEx::getLineName(const QString &name)
{
    currentSeries->setName(name);
}

void ChartViewEx::getLineWidth(int index)
{
    QPen pen = currentSeries->pen();
    pen.setWidth(index);
    currentSeries->setPen(pen);
}

void ChartViewEx::getColor(int index)
{
    QComboBox* cbBox = qobject_cast<QComboBox*> (sender());
    QStringList colorList = QColor::colorNames();

    QColor color =QColor(colorList[index]);
    if (cbBox ==cbBoxBkColor)
    {
        mChart->setPlotAreaBackgroundVisible(true);
        mChart->setPlotAreaBackgroundBrush(QBrush(color,Qt::SolidPattern));
    }

    if(cbBox == cbBoxLineColor)
    {
        currentSeries->setColor(color);
        QList<QLegendMarker*> lstMarker = mChart->legend()->markers(currentSeries);

        for(int i=0;i<lstMarker.count();i++)
        {
            QLegendMarker* marker = lstMarker.at(i);
            switch (marker->type()) {
            case QLegendMarker::LegendMarkerTypeXY:
            {

                QBrush brush;
                brush= marker->brush();
                color.setAlphaF(brush.color().alphaF());
                brush.setColor(color);
                marker->setBrush(brush);

                QPen pen;
                pen= marker->pen();
                color.setAlphaF(pen.color().alphaF());
                pen.setColor(color);
                marker->setPen(pen);
                break;
            }
            case QLegendMarker::LegendMarkerTypeArea:
            case QLegendMarker::LegendMarkerTypeBar:
            case QLegendMarker::LegendMarkerTypePie:
            case QLegendMarker::LegendMarkerTypeBoxPlot:
            case QLegendMarker::LegendMarkerTypeCandlestick:
            default:
            {
                break;
            }
            }
        }
    }

}

void ChartViewEx::setXAxisName(const QString &name)
{
    QFont font = mChart->axisX()->titleFont();
    font.setWeight(QFont::DemiBold);
    font.setPointSize(10);
    mChart->axisX()->setTitleFont(font);

    mChart->axisX()->setTitleVisible(true);
    mChart->axisX()->setTitleText(name);
}

void ChartViewEx::setYAxisName(const QString &name)
{
    QFont font =  mChart->axisY()->titleFont();
    font.setWeight(QFont::DemiBold);
    font.setPointSize(10);
    mChart->axisY()->setTitleFont(font);

    mChart->axisY()->setTitleVisible(true);
    mChart->axisY()->setTitleText(name);
}

void ChartViewEx::setChartName(const QString &name)
{
    QFont font = mChart->titleFont();
    font.setWeight(QFont::ExtraBold);
    font.setPointSize(16);
    mChart->setTitleFont(font);

    mChart->setTitle(name);
}

void ChartViewEx::ClearAll()
{
    mChart->removeAllSeries();
}

void ChartViewEx::checkPointValid(QPointF& pos)
{
    QPointF origin = mChart->plotArea().bottomLeft();
    QPointF rightTopP = mChart->plotArea().topRight();

    if(pos.x() < origin.x())
    {
        pos.setX(origin.x());
    }

    if(pos.x() >  rightTopP.x())
    {
        pos.setX(rightTopP.x());
    }

    if(pos.y() > origin.y())
    {
        pos.setY(origin.y());
    }

    if(pos.y() < rightTopP.y())
    {
        pos.setY(rightTopP.y());
    }
}

void ChartViewEx::saveImage()
{
    if(mChart->series().count() == 0)
        return;

    QPixmap map = this->grab(this->rect());

    QString newPath = QFileDialog::getSaveFileName(this, tr("Save Chart"),
                                                   "/chart", tr("JPG files (*.JPG)"));

    if (newPath.isEmpty())
        return;

    map.save(newPath,"JPG");
}

void ChartViewEx::SelectAllNorNot()
{
    QAction* action = qobject_cast<QAction*>(sender());

    bool isDisplay =false;
    if(action == selectAllCurve)
    {
        isDisplay = true;
    }

    if(action == hideAllCurve)
    {
        isDisplay = false;
    }

    QList<QLegendMarker*> lstMarker = mChart->legend()->markers();
    for(int i=0;i<lstMarker.count();i++)
    {
        QLegendMarker* marker = lstMarker.at(i);
        switch (marker->type()) {
        case QLegendMarker::LegendMarkerTypeXY:
        {
            marker->setVisible(isDisplay);
            marker->series()->setVisible(isDisplay);

            qreal aplha =1.0;
            if(!marker->series()->isVisible() && isLegendVisible)
            {
                aplha = 0.5;
                marker->setVisible(true);
            }

            QBrush brush;
            brush= marker->brush();
            QColor color;
            color= brush.color();
            color.setAlphaF(aplha);
            brush.setColor(color);
            marker->setBrush(brush);

            QPen pen;
            pen= marker->pen();
            color = pen.color();
            color.setAlphaF(aplha);
            pen.setColor(color);
            marker->setPen(pen);
            break;
        }
        case QLegendMarker::LegendMarkerTypeArea:
        case QLegendMarker::LegendMarkerTypeBar:
        case QLegendMarker::LegendMarkerTypePie:
        case QLegendMarker::LegendMarkerTypeBoxPlot:
        case QLegendMarker::LegendMarkerTypeCandlestick:
        default:
        {
            break;
        }
        }
    }

}

void ChartViewEx::markerVisible()
{
    isMarkerVisible = !isMarkerVisible;
    QList<QAbstractSeries*> lstSeries = mChart->series();
    for(int i=0;i<lstSeries.count();i++)
    {
        QLineSeries* series = qobject_cast<QLineSeries*>(lstSeries.at(i));
        series->setPointsVisible(isMarkerVisible);
    }
}

